﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WCFERP.PatcherContracts;
using WCFMINERP.BLL.Pacher;
using System.Xml;
using System.IO;

namespace WCFMINERP.Services.Patcher
{
    public class PatcherServer : IPatcher
    {
        PatcherBll _bll = new PatcherBll();
        /// <summary>
        /// 获取要升级的文件信息当获取不到时不升级
        /// 比较版本号 需不需要更新不需要更新也返回空
        /// </summary>
        /// <returns></returns>
        public PatherInfo GetFileInfo(float localVersion)
        {
            PatherInfo rtnInfo ;
            string updatePath = _bll.GetUpatePath();//存放升级文件的目录包含升级文件和相应的zip包信息
            if (!string.IsNullOrEmpty(updatePath))
            {
                //找到名字为update.xml的信息读出版本信息
                string xmlPath = updatePath + "\\update.xml";
                XmlDocument xml = new XmlDocument();
                if (File.Exists(xmlPath))
                {
                    xml.Load(xmlPath);
                    float serverVersion = Convert.ToSingle(xml.SelectSingleNode(@"Version/ClientVersion").InnerText);
                    string serverFileName = xml.SelectSingleNode(@"Version/FileName").InnerText;
                    string filePath = updatePath + "\\" + serverFileName;//更新文件存放的位置
                    FileStream stream = System.IO.File.OpenRead(filePath);
                    if (localVersion < serverVersion)//需要更新
                    {
                        rtnInfo = new PatherInfo();
                        ///组织信息
                        rtnInfo.Name = serverFileName;
                        rtnInfo.Length = stream.Length;
                        rtnInfo.ClientVersion = serverVersion;
                        //rtnInfo.Offset = 0;
                        //rtnInfo.Data
                        return rtnInfo;
                    }
                    else
                    {
                        //不需要更新
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 下载文件传递文件信息
        /// </summary>
        /// <returns></returns>
        public PatherInfo DownloadFile(PatherInfo inPather)//下载文件
        {
            PatherInfo rtnInfo=new PatherInfo();
            rtnInfo.Name=inPather.Name;
            rtnInfo.Length=inPather.Length;
            rtnInfo.Offset=inPather.Offset;

            int MaxSize = 500 * 1024;
            string updatePath = _bll.UpdatePath;
            FileStream fs = new FileStream(updatePath + "\\" + inPather.Name, FileMode.OpenOrCreate);//打开文件
            long Size = rtnInfo.Length - rtnInfo.Offset <= MaxSize ? rtnInfo.Length - rtnInfo.Offset : MaxSize;
            rtnInfo.Data = new byte[Size]; //设置传递的数据的大小
            //long offset = inPather.Offset;  //file.Offset 文件偏移位置,表示从这个位置开始进行开始读取
            fs.Position = inPather.Offset; //设置本地文件数据的读取位置
            fs.Read(rtnInfo.Data, 0, rtnInfo.Data.Length);//把数据写入到file.Data中
            fs.Close();
            return rtnInfo;

 
        }
    }
}
